package com.vectz.autogen.util;

import org.apache.log4j.Logger;
import org.apache.log4j.Priority;

/**
 * @author ai-t.vn
 *
 */
public final class Log {
	// Log levels, create fake ones if they don't match the Log4J standard ones

	public final static Priority NONE = Priority.DEBUG;

	public final static Priority SEVERE = Priority.FATAL;

	public final static Priority ERROR = Priority.ERROR;

	public final static Priority WARN = Priority.WARN;

	public final static Priority INFO = Priority.INFO;

	public final static Priority DEBUG = Priority.DEBUG;

	private static boolean bInitialized = false;

	private static Logger operation = null;

	private static final Log m_instance = new Log();

	protected Log() {
		initialize();
	}

	public final static Log instance() {
		return (m_instance);
	}

	/**
	 * Configures the Log4J system using the properties/Logger.properties file.
	 * Read the Log4J docs on how to setup one of these files to do anything you
	 * want. If this method isn't called before doing some logging, then Log4j
	 * will complain.
	 */
	private final static void initialize() {
		// don't bother if we are already initialized
		if (bInitialized) {
			return;
		}
		try {
			operation = Logger.getLogger("operation");
			bInitialized = true;
		} catch (Exception e) {
			System.err.println("Problem writing to log.");
			e.printStackTrace();
		} catch (Error er) {
			System.err.println("Problem writing to log.");
			er.printStackTrace();
		}
	}

	public final static void log(Priority pLogLevel, String sMessage) {
		try {
			initialize();
			operation.log(pLogLevel, sMessage);
		} catch (Exception e) {
			System.err.println("Problem writing to log.");
			e.printStackTrace();
		} catch (Error er) {
			System.err.println("Problem writing to log.");
			er.printStackTrace();
		}
	}

	public final static void log(String sMessage) {
		try {
			initialize();
			operation.log(INFO, sMessage);
		} catch (Exception e) {
			System.err.println("Problem writing to log.");
			e.printStackTrace();
		} catch (Error er) {
			System.err.println("Problem writing to log.");
			er.printStackTrace();
		}
	}
}
